$(function() {
	new Vue({
		el: '#goodListApp',
		data: {
			fullscreenLoading: false,
			activeName: 'first',
			searchGood: "",
			totalSize: 1,
			tableData: [],
			rebaseIds: [],
			currentPage: 1,
			dialogEditFormVisible:false,
			dialogEditActionFormVisible:false,
			editFrom:{
				dateStr:"",
				timeStr:"",
				id:"",
				logoId:"",
			},
			options:[],
			
		},
		created: function() {
			parent.vueData.$data.hreHtml = "../goodList/index.html";
			parent.vueData.activeIndex  = "6";
			parent.vueData.getLeftNav("6")
			parent.vueData.$data.categoryIndex = 3
			this.getGoodList(1, this.searchGood);
			this.getAllHomeMenu()
		},
		methods: {
			getGoodList: function(page, key) {
				this.fullscreenLoading = true;
				let param = {
					
					pageNum: page,
					keyword: key,
				}
				axios({
					url: POSTURL.selectAllCommodity,
					method: 'post',
					data: param
				}).then(response => {
					this.fullscreenLoading = false;

					if (response.data.code === 100) {
                        console.log(response.data);
						if (page === 1) {
							if (response.data.result.pageCount <= 1) {
								this.totalSize = response.data.result.commodities.length
							} else {
								this.totalSize = response.data.result.pageCount * 10
							}
							this.tableData.length = 0;
							this.tableData = [];
						}
						for (let i = 0; i < response.data.result.commodities.length; i++) {

							let value = response.data.result.commodities[i];

							let good = new Object();
						    good.name = value.name
							good.releaseDate = value.releaseDate
							good.uid = value.uid
							good.cover = value.cover
							good.newPrice = value.newPrice
							good.inventory = value.inventory
							good.isPutAway = !value.isPutAway
							good.categoryId = value.categoryId
							good.shop = value.shop
							good.faddishState  = value.faddishState 
							good.isHot  = value.isHot
							good.isNew  = value.isNew
							good.secondsKill  = value.secondsKill
							good.isRecommend  = value.isRecommend
							good.isRights = value.isRights
							good.showState = value.showState
							if (value.auditState === 0) {
								good.status = "审核通过";
							} else if (value.auditState === 1) {
								good.status = "审核中";
							} else if (value.auditState === 2) {
								good.status = "审核审核未通过";
							}
							good.logos = []
							for (let data of value.logos) {
								let  obj  = new Object();
								obj.name = data.name
								obj.id = data.id
								obj.goodId = value.uid
								good.logos.push(obj)
							}
							this.tableData.push(good)
						}
						console.log(this.tableData);
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			handleClick(tab, event) {
			},
			clickSearchGood() {
				if (stringEmpty(this.searchGood)) {
					this.$message.error('请输入关键字');
					return;
				}
				this.currentPage = 1
				this.getShopList(1, this.searchGood)
			},
			handleCurrentChange(val) {
				this.getGoodList(val, this.searchGood)
			},
			handeBlackGood(val){
				this.fullscreenLoading = true;
				let param = {
					data:val.uid
				}
				axios({
					url: POSTURL.blockGoods,
					method: 'post',
					data: param
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.getGoodList(1,this.searchGood);
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			//设置action（1首页图标、2好物钜惠、3限时特价、4秀品好货、5精挑细选、6掌柜权益、7热销排行）
			handleSetGoodClick(val,type){
				if(type == 1){
					this.dialogEditActionFormVisible = true
					this.editFrom.id = val.uid
					return
				}
				else if(type ==3){
					this.dialogEditFormVisible = true
					this.editFrom.id = val.uid
					return;
				}
				
				this.fullscreenLoading = true;
				let param = {
					commodityId: val.uid,
					activity: type,
				}
				axios({
					url: POSTURL.setActivityCommodity,
					method: 'post',
					data: param
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.$message({
							showClose: true,
							message: response.data.message,
							type: 'success'
						});
						 this.getGoodList(1,this.searchGood);
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			//设置限时
			configSetActionTime(){
				
				if(stringEmpty(this.editFrom.dateStr)){
					this.$message.error('请选择日期');
					return
				}
				if(stringEmpty(this.editFrom.timeStr)){
					this.$message.error('请选择时间');
					return
				}
				let endTime = this.editFrom.dateStr + " " + this.editFrom.timeStr 
				this.fullscreenLoading = true;
				let param = {
					commodityId: this.editFrom.id,
					activity: 3,
				}
				axios({
					url: POSTURL.setActivityCommodity,
					method: 'post',
					data: param
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.dialogEditFormVisible = false;
						this.$message({
							showClose: true,
							message: response.data.message,
							type: 'success'
						});
					   this.editFrom = {}
					   this.getGoodList(1,this.searchGood);
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			//设置首页活动
			configSetActionHome(){
                  console.log(this.editFrom);
				  if(stringEmpty(this.editFrom.logoId)){
				  	this.$message.error('请选择首页商品分类');
				  	return
				  }
				  this.fullscreenLoading = true;
				  let param = {
				  	commodityId: this.editFrom.id,
				  	activity:1,
					logoId:this.editFrom.logoId
				  }
				  axios({
				  	url: POSTURL.setActivityCommodity,
				  	method: 'post',
				  	data: param
				  }).then(response => {
				  	this.fullscreenLoading = false;
				  	if (response.data.code === 100) {
				  		this.dialogEditActionFormVisible = false;
				  		this.$message({
				  			showClose: true,
				  			message: response.data.message,
				  			type: 'success'
				  		});
				  	   this.editFrom = {}
				  	   this.getGoodList(1,this.searchGood);
				  	} else {
				  		this.$message.error(response.data.message);
				  	}
				  }).catch(error => {
				  	this.fullscreenLoading = false;
				  	console.log(error);
				  	this.$message.error('服务器出错!');
				  })
			},
			getAllHomeMenu(){
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.allHomepageLogo,
					method: 'post',
					data: {}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						for (let item of response.data.result) {
							let obj = new Object();
							obj.value = item.id;
							obj.label = item.name;
							this.options.push(obj)
						}
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			handleRelieveGood(val){
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.relieveGoods,
					method: 'post',
					data: {
							data:val.uid,
						  }
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						   this.getGoodList(1,this.searchGood);
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			//取消活动类型
			handleActionClick(item){
				  this.fullscreenLoading = true;
				  axios({
				  	url: POSTURL.cancelActivityCommodity,
				  	method: 'post',
				  	data: {
						commodityId:item.goodId,
						activity:1,
						logoId:item.id
					}
				  }).then(response => {
				  	this.fullscreenLoading = false;
				  	if (response.data.code === 100) {
				  		   this.getGoodList(1,this.searchGood);
				  	} else {
				  		this.$message.error(response.data.message);
				  	}
				  }).catch(error => {
				  	this.fullscreenLoading = false;
				  	console.log(error);
				  	this.$message.error('服务器出错!');
				  })
			},
			//取消类型
			handleCancelGoodClick(item,type){
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.cancelActivityCommodity,
					method: 'post',
					data: {
						commodityId:item.uid,
						activity:type,
					}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						   this.getGoodList(1,this.searchGood);
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			}
			
		},
	})
})
